//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package 力扣.题库.未分类;

public class demo_2012 {
    public demo_2012() {
    }

    public static void main(String[] args) {
        int[] num = new int[]{2, 4, 6, 4};
        System.out.println(sumOfBeauties(num));
    }

    public static int sumOfBeauties(int[] nums) {
        boolean tow = true;
        boolean one = true;
        int sum = 0;

        for(int i = 1; i <= nums.length - 2; ++i) {
            for(int j = i - 1; j >= 0; --j) {
                if (nums[j] >= nums[i]) {
                    tow = false;
                    break;
                }
            }

            if (tow) {
                for(int k = i + 1; k <= nums.length - 1; ++k) {
                    if (nums[k] <= nums[i]) {
                        tow = false;
                        break;
                    }
                }
            }

            one = nums[i - 1] < nums[i] && nums[i] < nums[i + 1];
            if (tow) {
                sum += 2;
            } else if (one) {
                ++sum;
            }
        }

        return sum;
    }

    public static int sumOfBeauties2(int[] nums) {
        int n = nums.length;
        int[] sufMin = new int[n];
        sufMin[n - 1] = nums[n - 1];

        for(int i = n - 2; i >= 0; --i) {
            sufMin[i] = Math.min(sufMin[i + 1], nums[i]);
        }

        int sum = 0;
        int preMax = nums[0];

        for(int i = 1; i < n - 1; ++i) {
            int x = nums[i];
            if (preMax < x && x < sufMin[i + 1]) {
                sum += 2;
            } else if (nums[i - 1] < x && x < nums[i + 1]) {
                ++sum;
            }

            preMax = Math.max(preMax, x);
        }

        return sum;
    }
}
